<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Tab;
use App\Models\Media;
use App\Models\Report;
use App\Models\Device;
use App\Models\Company;
use Encore\Admin\Controllers\HasResourceActions;
use Log;
use Faker\Factory;
use Dcat\Admin\Widgets\Dropdown;
use Dcat\Admin\Widgets\Card;
use App\Admin\Renderable\DeviceTable;
use Illuminate\Http\Request;
use Dcat\Admin\Form\Field\Editor;
// use Dcat\Admin\Layout\Content;
// use Dcat\Admin\Widgets\Form;


class ReportController extends Controller
{

    public function index(Content $content, $pid)
    {
        return $content
            ->header('检修报告列表')
            ->description()
            ->body($this->grid($pid));
    }

    protected function grid($pid)
    {
        return new Grid(Report::where(['project_id'=>$pid]), function (Grid $grid) {
            $grid->disableColumnSelector();
            $grid->column('id','ID');
            $grid->column('name','标题');
            // $grid->column('content','备注');
            // $grid->column('info','内容');
            $grid->column('created_at','创建时间');
            // 启用边框模式
            $grid->withBorder();
            $grid->fixColumns(2);
            //显示详情按钮
            // $grid->showViewButton();
            $grid->disableViewButton();
            //操作按钮
            $grid->showActions();
            // $grid->disableActions();
            // $grid->disableBatchDelete();
            // 创建按钮
            $grid->showCreateButton();
            // 禁用创建按钮
            // $grid->disableCreateButton();
            // 禁用详情按钮
            // $grid->disableViewButton();
            // 禁用编辑按钮
            // $grid->disableEditButton();
            // 显示编辑按钮
            $grid->showEditButton();
            // 禁用快捷编辑按钮
            $grid->disableQuickEditButton();
            // 显示快捷编辑按钮
            // $grid->showQuickEditButton();
            // 设置弹窗宽高，默认值为 '700px', '670px'
            // $grid->setDialogFormDimensions('50%', '60%');
            // $grid->enableDialogCreate();
            // 禁用删除按钮
            // $grid->disableDeleteButton();
            // 显示删除按钮
            $grid->showDeleteButton();
        });
    }

    public function update($pid,$id){
        return $this->form($pid)->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content,$pid){
        return $content
            ->title('新增检修报告')
            ->description('创建')
            ->body($this->form($pid));
    }
    public function form($pid = 0){
        return Form::make(new Report(), function (Form $form) use ($pid) {
            $form->hidden('project_id')->default($pid);
            $form->text('name','检修报告名称')->required();
            $form->radio('radio')->when(1, function (Form $form) {
                $form->editor('content', __('内容1'));
                $form->editor('info', __('内容2'));
            })->when(2, function (Form $form) {
                $form->file('path','检修报告文件')->removable(false)->autoUpload();
            })->options([
                1 => '模板填写',
                2 => '上传文件',
            ])->default(1);
            // $form->saving(function (Form $form){

            // });
        });
    }

    public function edit($pid,$id,Content $content){
        return $content
            ->title('检修报告编辑')
            ->description('编辑')
            ->body($this->form($pid)->edit($id));
    }

    public function show($id){
        return Show::make($id, new Media(), function (Show $show) {
            // $show->layerId;
            $show->field('title','项目名称');
            // $show->field('remark','公司备注');
            $show->created_at;
        });
    }
    //审核提交
    public function postAudit(Request $request){
        $info = $request->all();
        $media = Media::where(['id'=>$info['id'],'status'=>0])->first()->toArray();
        if ($media) {
            $param['admin_id'] = $info['aid'];
            if(isset($info['reason'])){
                $param['status'] = 2;
                $param['reason'] = $info['reason'];
            }else{
                $param['status'] = 1;
            }
            Media::where(['id'=>$info['id']])->update($param);
            return array('msg'=>'审核成功！','status'=>1);
        }else{
            return array('msg'=>'审核失败！','status'=>0);
        }
    }
}